import '../model/auth_model.dart';
import '../model/result_model.dart';
import '../utils/request.dart';

///认证API
class AuthApi {
  AuthApi._();

  static final _signleton = AuthApi._();

  factory AuthApi() => _signleton;

  ///图形验证码
  Future<AuthCaptchaRes> captcha() => request
      .getHandler(path: '/captcha', useToken: false)
      .then((res) => AuthCaptchaRes.fromResult(res));

  ///用户登录
  Future<AuthLoginRes> login(final AuthLoginReq req) => request
      .postHandler(
        path: '/${req.tenantCode}/login',
        useToken: false,
        data: req.toMap(),
      )
      .then((res) => AuthLoginRes.fromResult(res));

  ///刷新令牌
  Future<RefreshTokenRes> refresh(final RefreshTokenReq req) => request
      .putHandler(path: '/refresh', params: req.toMap())
      .then((res) => RefreshTokenRes.fromResult(res));

  ///修改密码
  Future<BaseModelRes> modifyPasswd(final ModifyPasswdReq req) =>
      request.putHandler(path: '/modify-passwd', data: req.toMap());

  /// 发送短信
  Future<BaseModelRes> sendSmsCode(final AuthSendSmsReq req) =>
      request.postHandler(path: '/${req.tenantCode}/sms', data: req.toMap());

  ///忘记密码
  Future<BaseModelRes> forgotPasswd(final AuthForgotPasswdReq req) =>
      request.postHandler(path: '/forgot-passwd', data: req.toMap());

  ///退出
  Future<BaseModelRes> logout() => request.deleteHandler(path: '/logout');
}

///认证接口单例
final authApi = AuthApi();
